﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain.OutboundShipments;
using B.S.DataBase.MyProject.Domain.WhereHorse;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Write.API.Applicantions.Command.PurchaseInStroeCommand;

namespace T.E.ERP.Write.API.Applicantions.CommandHandler.PurchaseInStroeCommandHandler
{
    public class ApplyPurchaseInStoreCommandHandler : IRequestHandler<ApplyPurchaseInStoreCommand, APIResult<object>>
    {
        private readonly IMapper mapper;
        private readonly IBaseRepository<PurchaseInRepository> purchaseInBiz;

        public ApplyPurchaseInStoreCommandHandler(IMapper mapper, IBaseRepository<PurchaseInRepository> purchaseInBiz)
        {
            this.mapper = mapper;
            this.purchaseInBiz = purchaseInBiz;
        }

        public Task<APIResult<object>> Handle(ApplyPurchaseInStoreCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            var infos = purchaseInBiz.Quary(x => x.Id == request.Id).Result.FirstOrDefault();
            var info = mapper.Map<PurchaseInRepository>(infos);
            info.DocumentStatus = request.ExamStatus;
            info.ExamUserName = request.ExamPersonName;
            int i = purchaseInBiz.Update(info).Result;
            if (i > 0)
            {
                result.Code = APIEnum.修改成功;
                result.Msg = APIEnum.修改成功.ToString();
                result.data = i;
            }
            else
            {
                result.Code = APIEnum.修改失败;
                result.Msg = APIEnum.修改失败.ToString();
            }

            return Task.FromResult(result);
        }
    }
}
